﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClassLibrary.Data;
using ClassLibrary.Entity;
using System.Data;

namespace ClassLibrary.DAO
{
    public class TblImageDAO
    {
        DBService _db = null;
        public TblImageDAO(string ConnectionString)
        {
            _db = new DBService(ConnectionString);
        }

        // Insert Hình Ảnh
        public bool InsImageItem(TblImage image)
        {
            bool flag = false;
            try
            {
                _db.ExeSP_NonQuery("sp_TblColorImage_InsUp",
                    new string[] { "@ItemID", "@ImageURL", "@ListImage", "@ColorName", "@ImageColor", "@IsDefault" },
                    new object[] { image.ItemID, image.ImageURL, image.ListImage, image.ColorName, image.ImageColor, image.IsDefault },
                    CommandType.StoredProcedure);
                flag = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return flag;
        }

        // Load Image By ID
        public TblImage GetImageByItemID(int ItemID)
        {
            TblImage colorImg = new TblImage();
            try
            {
                DataTable dt = _db.ExeSP_DataTable("select * from TblImage where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID }
                    , CommandType.Text);
                foreach (DataRow dr in dt.Rows)
                {
                    colorImg.ListImage = dr["ListImage"].ToString();
                    colorImg.ImageColor = dr["ImageColor"].ToString();
                    colorImg.ColorName = dr["ColorName"].ToString();
                    colorImg.IDImage = Convert.ToInt32(dr["IDImage"].ToString());
                    colorImg.ImageURL = dr["ImageURL"].ToString();
                    colorImg.IsDefault = Convert.ToInt32(dr["IsDefault"].ToString());
                    colorImg.ItemID = ItemID;
                    
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return colorImg;
        }

        public DataTable GetImgItems(int ItemID)
        {
            DataTable dt = null;
            try
            {
                dt = _db.ExeSP_DataTable("select * from TblImage where ItemID = @ItemID",
                    new string[] { "@ItemID" },
                    new object[] { ItemID },
                    CommandType.Text);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }
    }
}
